const connection = require('../app/database');
class UserService { // service/userService.js
  async create (user) { // 创建用户
    // 将user存储到数据库中
    const { name, password } = user; // 结构获取值
    const statememt = `INSERT INTO users (name, password) VALUES (?, ?);`; // sql语句
    const result = await connection.execute(statememt, [name, password]) // 连接数据库进行写入 Promise
    return result[0];
  }
  async getUserByName (name) { // 检查是否有改用户
    const statement = `SELECT * FROM users WHERE name = ?;`;
    const result =  await connection.execute(statement, [name]);
    return result[0];
  }
  async updateAvatarUrlById (avatarUrl , userId) {
  const statement = `UPDATE users SET avatar_url = ? WHERE id = ?;`;
  const [result] = await connection.execute(statement, [avatarUrl, userId]);
  return result;
  }
}
module.exports = new UserService();